﻿using MusicPlayerPlus.Interface.Base;
using MusicPlayerPlus.Interface.Model.Muisc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MusicPlayerPlus.PlugIn.GetXiami
{
    public class PlugIn : InterfaceBase,
        //Input
        MusicPlayerPlus.Interface.Base.Action.Input.IAutoExecuteAction,
        //Output
        MusicPlayerPlus.Interface.Base.Action.Output.IAddPlayListAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IChangeMusicListAction
    {

        /// <summary>
        ///虾米播放列表的guid
        /// </summary>
        public static Guid Xiami_PlayList = new Guid("{00000001-0001-0002-0001-000000000001}");

        /// <summary>
        /// 自动执行动作接口
        /// </summary>
        public void AutoExecute()
        {
            //Dictionary<string, string> xiamiUrl = new Dictionary<string, string>();
            //xiamiUrl.Add("虾米音乐", "http://www.xiami.com/song/play?ids=/song/playlist/id/1769382897%2C1769577231%2C1770554731%2C3500523%2C3501622%2C1769356072%2C1769356032%2C1771217681%2C1769567881%2C1771315137%2C1770082556%2C1770275176%2C1769242422%2C1769866643%2C1771194184%2C3442950%2C1770480302%2C1770691279%2C1771194180%2C1770934558%2C1770103824%2C1770239267%2C1770733013%2C3592888%2C3592887/object_name/default/object_id/0");
            //xiamiUrl.Add("常听列表", "http://www.xiami.com/song/play?ids=/song/playlist/id/12956956/type/3");

            //MusicPlayerPlus.Method.Data.Settings.SaveSettings("XiamiUrl", xiamiUrl);

            Dictionary<string, string> xiamiUrl = MusicPlayerPlus.Method.Pubic.StaticData.Settings.GetSettings<Dictionary<string, string>>("XiamiUrl");

            if (xiamiUrl == null)
            {
                return;
            }
            foreach (var item in xiamiUrl)
            {
                GetXiamiMusic(item.Key, item.Value);
            }
        }

        /// <summary>
        /// 添加播放列表
        /// </summary>
        public Func<PlayList, bool> AddPlayList_Action { private get; set; }
        /// <summary>
        /// 改变列表
        /// </summary>
        public Action<Guid> ChangeMusicList_Action { private get; set; }


        private void GetXiamiMusic(string title, string url)
        {
            AMing.Core.Method.ThreadHelper threadHelper = new AMing.Core.Method.ThreadHelper();
            threadHelper.Thread_Background(() =>
            {
                XiamiDecode.Decode decode = new XiamiDecode.Decode();
                var data = decode.GetTrackData(url);
                if (data != null)
                {
                    PlayList xiamilist = new PlayList()
                    {
                        ID = Guid.NewGuid(),
                        Name = title,
                        Muisic = new Dictionary<Guid, MusicItem>(),
                        Type = Interface.Model.Enum.PlayListType.Temp
                    };
                    foreach (var item in data.TrackList)
                    {
                        MusicItem newitem = new MusicItem()
                        {
                            ID = Guid.NewGuid(),
                            Name = item.Title,
                            AlbumName = item.Album_name,
                            Url = item.MP3Url,
                            Cove = item.Pic_2,
                            CoveHD = item.Pic_4,
                            CoveThumbnail = item.Pic_3,
                            Type = Interface.Model.Enum.MusicSourceType.Web
                        };

                        if (!xiamilist.Muisic.ContainsKey(newitem.ID))
                        {
                            xiamilist.Muisic.Add(newitem.ID, newitem);
                        }
                    }
                    AddPlayList_Action(xiamilist);
                }
            });
        }

    }
}